package netcode.helper;

public class NetcodeHelper {
	public static final String SERVER_ADDRESS = "banana.cs.uwp.edu";
	public static final int SERVER_PORT = 54540;
	// This goes in the first byte of a message
	public static final byte BRAINS_AND_BETS = 0x2B;
	// This goes in the second byte of a message
	public static final byte MESSAGE_VERSION = 0x00;
	// Packet Codes(Tied to the third byte of a message)
	public static final byte CONNECTION_REQUEST = 0x00;
	public static final byte CONNECTION_RESPONSE = 0x01;
	public static final byte SERVER_LOBBY_INFO_REQUEST = 0x02;
	public static final byte SERVER_LOBBY_INFO_RESPONSE = 0x03;
	public static final byte JOIN_GAME_REQUEST = 0x04;
	public static final byte GAME_LOBBY_INFO = 0x05;
	public static final byte CREATE_GAME_REQUEST = 0x06;
	public static final byte START_GAME_REQUEST = 0x07;
	public static final byte QUESTION = 0x08;
	public static final byte ANSWER = 0x09;
	public static final byte BETTING_ODDS = 0x0A;
	public static final byte SUBMIT_BETS = 0x0B;
	public static final byte ROUND_RESULTS = 0x0C;
	public static final byte NEXT_ROUND_REQUEST = 0x0D;
	public static final byte LEAVE_GAME_REQUEST = 0x0E;
	public static final byte GAME_RESULTS = 0x0F;
	public static final byte DISCONNECT = 0x10;
	public static final byte ARE_YOU_THERE = 0x11;
	public static final byte ERROR = 0x12;
	public static final byte MESSAGE_RECEIVED = 0x13;
	public static final byte JOIN_GAME_INVITE = 0x14;
	
	public static final int WAITING_TO_START = 0;
	public static final int STARTED = 1;
	public static final int ENDED = 2;
	public static final int MIN_N_PLAYERS = 1;
	public static final int MAX_N_PLAYERS = 6;
	
	/*
	 * Error Message Codes
	 */
	public static final byte ERROR_BAD_GAME = 0x01;
	public static final byte ERROR_BAD_VER = 0x02;
	public static final byte ERROR_BAD_TYPE = 0x02;
	public static final byte ERROR_BAD_MESSAGE = 0x03;
	public static final byte ERROR_BAD_CLIENTID = 0x04;
	public static final byte ERROR_BAD_GAMEID = 0x05;
	public static final byte ERROR_FULL_GAME = 0x06;
	public static final byte ERROR_GAME_IN_PROGRESS = 0x07;
	public static final byte ERROR_GAME_NOT_FOUND = 0x08;
	public static final byte ERROR_END_OF_MESSAGE = 0x09;
	//Why 0x7F for the last one? Because
	//0XFF throws an error about it not being an int.
	public static final byte ERROR_OTHER = 0x7F;
	
	public static final int DATA_OFFSET = 3;
	public static final int BB_MESSAGE_ID_INDEX = 0;
	public static final int BB_MESSAGE_VERSION_INDEX = 1;
	public static final int BB_MESSAGE_TYPE_INDEX = 2;
	public static final int MAX_MESSAGE_LENGTH = 8192;

	public static final byte END_OF_STRING = 0x00;
	public static final byte END_OF_RECORD = 0x03;
	public static final byte END_OF_MESSAGE = 0x04;
	
	public static final int NUMBER_OF_SENDING_MESSAGE_ATTEMPTS = 10;
	public static final int MILLISECONDS_BETWEEN_SENDING_MESSAGE = 100;

	public static final byte[] intToByteArray(int value) {
		return new byte[] { (byte) (value >>> 24), (byte) (value >>> 16),
				(byte) (value >>> 8), (byte) value };
	}

	public static final int byteArrayToInt(byte[] b) {
		return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8)
				+ (b[3] & 0xFF);
	}
	
	public static final byte[] generateMessageByteArray(byte code){
		byte message[] = new byte[MAX_MESSAGE_LENGTH];
		message[BB_MESSAGE_ID_INDEX] = BRAINS_AND_BETS;
		message[BB_MESSAGE_VERSION_INDEX] = MESSAGE_VERSION;
		message[BB_MESSAGE_TYPE_INDEX] = code;
		return message;
	}
}
